<?php
/**
 * Created by PhpStorm.
 * User: Mr.Li <lhb2002@qq.com>
 * Date: 2019/5/28
 * Time: 13:58
 */
namespace LHB_Dycdp;

use LHB_Dycdp\Gateways\CreateCdpOrder;
use LHB_Dycdp\Gateways\QueryCdpOffer;
use LHB_Dycdp\Gateways\QueryCdpOfferById;
use LHB_Dycdp\Gateways\QueryCdpOrder;
use LHB_Dycdp\Gateways\ReceiveAlicomMsg;

class Dycdp
{

    protected $accessKeyId ;
    protected $accessKeySecret ;
    //扩展参数
    protected $extend = [];
    private function __construct($accessKeyId,$accessKeySecret,...$extend)
    {
        $this->accessKeyId = $accessKeyId;
        $this->accessKeySecret = $accessKeySecret;
        $this->extend = $extend[0];
    }

    public static function create($config){
        if( !array_key_exists('accessKeyId',$config) || !array_key_exists('accessKeySecret',$config)){
            throw new Exception('accessKeyId和accessKeySecret为必要参数');
        }
        $instance = new self($config['accessKeyId'],$config['accessKeySecret'],$config);
        return $instance;
    }

    /**
     * 查询商品信息
     * @param $channelType  通道类型	全国、分省
     * @param $vendor 运营商
     * @param $province 省份
     * User: Mr.Li <lhb2002@qq.com>
     * Date: 2019/5/28
     * Time: 14:31
     */
    public function queryCdpOffer($channelType='全国',$vendor='',$province=''){
        $gateways = new QueryCdpOffer($this->accessKeyId,$this->accessKeySecret);
        return $gateways->handle(compact('channelType','vendor','province'));
    }


    /**
     * 查询商品价格
     * @param $offerId 平台商品
     * User: Mr.Li <lhb2002@qq.com>
     * Date: 2019/5/28
     * Time: 14:55
     */
    public function queryCdpOfferById($offerId){
        $gateways = new QueryCdpOfferById($this->accessKeyId,$this->accessKeySecret);
        return $gateways->handle(compact('offerId'));
    }

    /**
     * 流量充值
     * @param $phoneNumber 待充值的手机号码
     * @param $offerId 待充的商品
     * @param $outOrderId 我们平台的订单
     * @param string $reason 备注信息
     * @param null $maxPrice 最大价格
     * @param array $paramList 扩展参数，暂时未使用
     * User: Mr.Li <lhb2002@qq.com>
     * Date: 2019/5/28
     * Time: 14:57
     */
    public function createCdpOrder($phoneNumber, $offerId, $outOrderId, $reason = '', $maxPrice = null, $paramList = array()){
        $gateways = new CreateCdpOrder($this->accessKeyId,$this->accessKeySecret);
        return $gateways->handle(compact('phoneNumber', 'offerId', 'outOrderId', 'reason', 'maxPrice'));
    }

    /**
     * 充值状态查询
     * @param $outOrderId 我们的订单号码
     * User: Mr.Li <lhb2002@qq.com>
     * Date: 2019/5/28
     * Time: 15:01
     */
    public function queryCdpOrder($outOrderId){
        $gateways = new QueryCdpOrder($this->accessKeyId,$this->accessKeySecret);
        return $gateways->handle(compact('outOrderId'));
    }


    public function receiveAlicomMsg(\Closure $callback){

        if( !array_key_exists('endPoint',$this->extend) || !array_key_exists('queueName',$this->extend) ){
            throw new Exception('endPoint和queueName为必要参数');
        }

        $gateways = ReceiveAlicomMsg::getInstance(
            $this->accessKeyId,
            $this->accessKeySecret,
            $this->extend['endPoint'],
            $this->extend['queueName']
        );
        return $gateways->run($callback);

    }
}